/*
 * Copyright 2009-2010 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.application.objectsource;

import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;

import org.kommando.application.core.Application;
import org.kommando.application.core.ApplicationProvider;
import org.kommando.core.catalog.AbstractObjectSource;
import org.kommando.core.catalog.CatalogObject;
import org.kommando.core.catalog.SimpleCatalogObject;
import org.kommando.core.util.Assert;

/**
 * 
 * @author Peter De Bruycker
 */
public class ApplicationObjectSource extends AbstractObjectSource {

    private static final String ID = "applications";

    private ApplicationProvider applicationProvider;

    private List<CatalogObject> applicationObjects;

    public ApplicationObjectSource(ApplicationProvider applicationProvider) {
        Assert.argumentNotNull("applicationProvider", applicationProvider);

        this.applicationProvider = applicationProvider;
    }

    public ApplicationProvider getApplicationProvider() {
        return applicationProvider;
    }

    @Override
    public String getDescription() {
        return "Indexes installed applications";
    }

    @Override
    public Icon getIcon() {
        // TODO icon
        return null;
    }

    @Override
    public String getId() {
        return ID;
    }

    @Override
    public String getName() {
        return "Applications";
    }

    @Override
    public List<CatalogObject> getObjects() {
        if (applicationObjects == null) {
            applicationObjects = new ArrayList<CatalogObject>();

            for (Application application : applicationProvider.getApplications()) {
                SimpleCatalogObject applicationObject = new SimpleCatalogObject("application:" + application.getName(), application.getName(), application
                        .getDescription(), application.getIcon());
                applicationObject.setExtension(Application.class, application);
                applicationObjects.add(applicationObject);
            }
        }

        return applicationObjects;
    }

}
